<?php
/**
 * Created by 日志观察者.
 * User: 楚羽幽
 * Date: 2018/2/8
 * Time: 0:38
 * QQ：1113581489
 */
namespace app\common\logic\observer\logic;
use IGetPush\IGeTui;
use think\Db;

class PushNotify implements \SplObserver
{
    //预设结果集
    private $data;
    //执行结果
    private $resutl = [];

    //配置
    protected $config;

    //构造函数
    public function __construct($data = [])
    {
        $this->data = $data;
        require_once WEB_PATH_EXTEND."/IGetPush/IGt.Push.php";
        $this->config   = [
            'member'    => [
                'host'                  => 'http://sdk.open.api.igexin.com/apiex.htm',
                'appid'                 => 'hO1SCBp4gv57m9We0qBIQ7',
                'secret'                => '16f6ZrRntz6vvtZCdq3Z3',
                'appkey'                => '2SZ7UyuRN38Zj4bIORgpL1',
                'mastersecret'          => 'KDwaRT8FR8Anwv7fAgG2N9'
            ],
            'staff'     => [
                'host'                  => 'http://sdk.open.api.igexin.com/apiex.htm',
                'appid'                 => 'GMd0DTLXwf6pIAXOcW0CI2',
                'secret'                => '7nWIaROS876LAwZ2BqWSE4',
                'appkey'                => 'In12kTnMYI8ay3m40Be2H9',
                'mastersecret'          => 'VHxeUISPWr9PSdFccNDWV5'
            ]
        ];
    }

    //获取结果集
    public function GetData()
    {
        return $this->data;
    }

    //更新操作
    public function update(\SplSubject $subject)
    {
        if (!empty($this->data))
        {
            //数据验证
            $msginfo = [
                'code'  => 1,
                'msg'   => ''
            ];
            if (!isset($this->data['token']) || empty($this->data['token'])) {
                $msginfo['msg'] = '缺少token';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['clientid']) || empty($this->data['clientid'])) {
                $msginfo['msg'] = 'clientid';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['type']) || empty($this->data['type'])) {
                $msginfo['msg'] = '缺少type';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['content']) || empty($this->data['content'])) {
                $msginfo['msg'] = '缺少content';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['notifytitle']) || empty($this->data['notifytitle'])){
                $msginfo['msg'] = '缺少notifytitle';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['notifytext']) || empty($this->data['notifytext'])) {
                $msginfo['msg'] = '缺少notifytext';
                $this->resutl = $msginfo;
                return false;
            }
            if (!isset($this->data['notifyurl']) || empty($this->data['notifyurl'])){
                $msginfo['msg'] = '缺少notifyurl';
                $this->resutl = $msginfo;
                return false;
            }

            //数据获取
            $host = $this->config[$this->data['type']]['host'];
            $appid = $this->config[$this->data['type']]['appid'];
            $appkey = $this->config[$this->data['type']]['appkey'];
            $master = $this->config[$this->data['type']]['mastersecret'];
            $clientid = $this->data['clientid'];
            $content = $this->data['content'];
            $notifytitle = $this->data['notifytitle'];
            $notifytext = $this->data['notifytext'];
            $notifyurl = $this->data['notifyurl'];
            $notifytype = isset($this->data['notifytype']) ? $this->data['notifytype'] : 1;

            $info = $this->pushMessageToSingle($host,$appid,$appkey,$master,$clientid,$content,$notifytitle,$notifytext,$notifyurl,$notifytype);
            if (isset($info['result']) && $info['result'] == 'ok' && isset($info['taskId']))
            {
                $msginfo['msg'] = '推送通知成功';
                $msginfo['code'] = 0;
                $this->resutl = $msginfo;
                return false;
            }else{
                $msginfo['msg'] = '推送通知失败';
                $this->resutl = $msginfo;
                return false;
            }
        }
    }

    //回调方法返回结果状态
    public function push_callback()
    {
        return $this->resutl;
    }

    /**
     * 推送通知
     * @param $host
     * @param $appid
     * @param $appkey
     * @param $master
     * @param $clientid
     * @param $content
     * @param $notifytitle
     * @param $notifytext
     * @param $notifyurl
     * @return \IGetPush\Array
     */
    private function pushMessageToSingle($host,$appid,$appkey,$master,$clientid,$content,$notifytitle,$notifytext,$notifyurl,$notifytype)
    {
        $igt = new IGeTui($host,$appkey,$master,false);

        //消息模版：
        if ($notifytype == 0)
        {
            $template = $this->IGtTransmissionTemplateDemo($appid,$appkey,$content);
        }else{
            $template = $this->IGtNotificationTemplateDemo($appid,$appkey,$content,$notifytitle,$notifytext,$notifyurl);
        }

        //定义"SingleMessage"
        $message = new \IGtSingleMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        //接收方
        $target = new \IGtTarget();
        $target->set_appId($appid);
        $target->set_clientId($clientid);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            return $rep;
        }catch(\RequestException $e){
            $requstId = $e->getRequestId();
            //失败时重发
            $igt->pushMessageToSingle($message, $target,$requstId);
        }
    }

    //打开应用模版
    private function IGtNotificationTemplateDemo($appid,$appkey,$content,$notifytitle,$notifytext,$notifyurl)
    {
        $template =  new \IGtNotificationTemplate();
        $template->set_appId($appid);                      //应用appid
        $template->set_appkey($appkey);                    //应用appkey
        $template->set_transmissionType(1);               //透传消息类型
        $template->set_transmissionContent($content);   //透传内容
        $template->set_title($notifytitle);                     //通知栏标题
        $template->set_text($notifytext);        //通知栏内容
        $template->set_logo(WEB_URL_IMG.'/app-logo.png');                  //通知栏logo
        $template->set_logoURL($notifyurl); //通知栏logo链接
        $template->set_isRing(true);                      //是否响铃
        $template->set_isVibrate(true);                   //是否震动
        $template->set_isClearable(true);                 //通知栏是否可清除
        return $template;
    }

    //透传消息模版
    private function IGtTransmissionTemplateDemo($appid,$appkey,$content){
        $template =  new \IGtTransmissionTemplate();
        //应用appid
        $template->set_appId($appid);
        //应用appkey
        $template->set_appkey($appkey);
        //透传消息类型
        $template->set_transmissionType(1);
        //透传内容
        $template->set_transmissionContent($content);
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        //这是老方法，新方法参见iOS模板说明(PHP)*/
        //$template->set_pushInfo("actionLocKey","badge","message","sound","payload","locKey","locArgs","launchImage");
        return $template;
    }
}